package com.controller;

import com.alibaba.fastjson2.JSONObject;
import com.config.Msg;
import com.dao.db1.LoginDao;
import com.pojo.User;
import com.vo.UserRolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class LoginController {
    private final LoginDao loginDao;

    @Autowired
    public LoginController(LoginDao loginDao) {
        this.loginDao = loginDao;
    }

    @RequestMapping("/login/Purposes")
    @ResponseBody
    public Msg selectPurposes(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String userName = jsonObject.getString("userName");
        String userPassword = jsonObject.getString("userPassword");
        List<User> users = loginDao.loginIndex(userName, userPassword);
        if (users.isEmpty()) {
            msg.setCode(500);
            msg.setMessage("账号或密码错误");
            return msg;
        }
        if (users.get(0).getStatus()==0){
            msg.setCode(500);
            msg.setMessage("账号异常,请联系管理员");
            return msg;
        }
        msg.setCode(200);
        msg.setMessage("登录成功");
        return msg;
    }

    // 获取所有权限
    @RequestMapping("/get/Permission")
    @ResponseBody
    public List<UserRolePermission> getPermission(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");

        return loginDao.getPermission(userName);
    }
}
